home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / PROGNG_C / TPTC17GS.LZH / TPTCSYS.PAS < prev    next >
Pascal/Delphi Source File  |  1988-05-02  |  14KB  |  585 lines

  1.  
  2. (*
  3.  * TPTCSYS.PAS - System unit for use with Turbo Pascal --> C Translator
  4.  *
  5.  * (C) 1988 S.H.Smith (rev. 07-Apr-88)
  6.  *
  7.  * This unit is compiled to create 'TPTCSYS.UNS' and 'TPTCSYS.UNH', which
  8.  * are automatically loaded on each TPTC run.   It defines the predefined
  9.  * environment from which programs are translated.
  10.  *
  11.  * Compile with:
  12.  *    tptc tptcsys -L -NU
  13.  *
  14.  * Note: the special 'as replacement_name' clause used in some cases.
  15.  * When present, this clause causes the replacement_name to be used in
  16.  * place of the original name in the translated output.
  17.  *
  18.  * Note: the special 'symtype <typename>' clause forces the declared symbol 
  19.  * type to the specified typename.  This is used when adding detail to 
  20.  * standard builtin identifiers (such as text).
  21.  *
  22.  * Lines starting with "\" are passed directly to the C object file
  23.  * without any translation.
  24.  *
  25.  *)
  26.  
  27. unit tptcsys;
  28.  
  29. interface
  30.  
  31.    const
  32.       maxint = $7fff;
  33.  
  34.  
  35. (*
  36.  * C language declarations passed directly to TPTCSYS.UNH
  37.  *
  38.  *)
  39.  
  40. \  #undef extern
  41.  
  42. \  #include <stdio.h>
  43.  
  44. \  #ifdef IN_TPTCSYS
  45. \  #define extern
  46. \  #endif
  47.  
  48. \  #define chr(n)          ((char)(n))
  49. \  #define integer         int
  50. \  #define word            unsigned
  51. \  #define longint         long
  52. \  #define real            double
  53. \  #define boolean         int
  54. \  #define false           0
  55. \  #define true            1
  56. \  #define nil             NULL
  57. \  #define STRSIZ 255      /* default string length */
  58.  
  59. \  /* set support */
  60. \  #define __  -2    /* thru .. */
  61. \  #define _E  -1    /* end of set marker */
  62.  
  63.  
  64.    type
  65.       byte = 0..$FF;
  66.       shortint = byte;
  67.       pointer = ^char;
  68.       setrec = pointer;
  69. \     typedef char *string;
  70.  
  71.       text = record    
  72.          fname:   string[64];    (* the actual filename *)
  73. \        FILE     *filevar;      /* C's file variable */
  74.       end symtype text;
  75.  
  76.    var
  77.       Output:     text;
  78.       Input:      text;
  79.       ParamCount: integer;
  80.       IoResult:   word;
  81.       tptc_argv:  ^pointer;
  82.       dseg:       word;
  83.       sseg:       word;
  84.       cseg:       word;
  85.  
  86.  
  87.    (* 
  88.     * Standard procedures and functions
  89.     *
  90.     *)
  91.    
  92.    function Sin(n: real): real;
  93.    function Cos(n: real): real;
  94.    function Tan(n: real): real;
  95.    function Sqr(n: real): real;
  96.    function Sqrt(n: real): real;
  97.    function Trunc(r: real): longint;
  98.    function Round(r: real): real;
  99.    function Int(r: real): real            as dint;
  100.  
  101.    function Pred(b: integer): integer;
  102.    function Succ(b: integer): integer;
  103.    function Ord(c: char): integer;
  104.    function Hi(w: word): word;
  105.    function Lo(w: word): word;
  106.    procedure Inc(var b: byte);
  107.    procedure Dec(var b: byte);
  108.  
  109.    function MemAvail: longint;
  110.    function MaxAvail: longint;
  111.    procedure Dispose(ptr: pointer);
  112.    procedure FreeMem(ptr: pointer; size: integer) as PfreeMem;
  113.       
  114.    function ParamStr(n: integer): string;
  115.    function UpCase(c: char): char;
  116.    procedure Delete(s: string; posit,number: integer);
  117.    function Copy(s: string; from,len: integer): string;
  118.    procedure Val(s: string; var res: real; var code: integer);
  119.    procedure Move(tomem, fmmem: pointer; bytes: word);
  120.    procedure FillChar(dest: pointer; size: integer; value: char);
  121.    function Length(s: string): integer;
  122.  
  123.  
  124.    (*
  125.     * These will probably change when binary file translation is implemented
  126.     *
  127.     *)
  128.  
  129.    function Eof(var fd: text): boolean;
  130.    procedure Flush(var fd: text);
  131.    procedure Close(var fd: text);
  132.    procedure Assign(var fd: text; name: string);
  133.    procedure Reset(var fd: text);
  134.    procedure ReWrite(var fd: text);
  135.    procedure Append(var fd: text);
  136.    procedure SetTextBuf(var fd: text; buffer: pointer; size: word);
  137.    procedure Seek(var fd: text; rec: word);
  138.    function SeekEof(var fd: text): boolean;
  139.  
  140.  
  141.  
  142.    (* 
  143.     * Additional procedures called by translated code 
  144.     *
  145.     *)
  146.    
  147. \  setrec setof(byte element,...);
  148.       {construct a set; variable parameter list}
  149.  
  150.    function inset(item: byte; theset: setrec): boolean;
  151.       {is an item a member of a set?}
  152.  
  153.    function spos(str1, str2: string): integer;
  154.       {returns the position of a substring within a longer string}
  155.       
  156.    function cpos(c: char; str2: string): integer;
  157.       {returns the position of a character within a string}
  158.       
  159.    function ctos(c: char): string;
  160.       {convert a character into a string}
  161.  
  162.       
  163. \  string scat(string control, ...);
  164.       {concatenate strings according to printf style control and
  165.        return pointer to the result}
  166.        
  167. \  void sbld(string dest,
  168. \            string control, ...);
  169.       {build a string according to a control string (works like sprintf
  170.        with with special handling to allow source and destination
  171.        variables to be the same)}
  172.        
  173. \  int tscanf(text *fd,
  174. \             string control, ...);
  175.       {functions like fscanf but allows whole-line reads into
  176.        string variables}
  177.        
  178. \  void tprintf(text *fd,
  179. \               string control, ...);
  180.       {functions like fprintf}
  181.        
  182.  
  183.  
  184.    (* The following identfiers are 'builtin' to the translator and
  185.       should not be defined here.  If any of these are redefined, the
  186.       corresponding special translation will be disabled. *)
  187.       
  188.    (* 
  189.     *   function Pos(key: string; line: string): integer;
  190.     *   procedure Chr(i: integer): char;
  191.     *   procedure Str(v: real; dest: string);
  192.     *   procedure Exit;
  193.     *
  194.     *   var 
  195.     *      Mem:    array[0..$FFFF:0..$FFFF] of byte;
  196.     *      MemW:   array[0..$FFFF:0..$FFFF] of word;
  197.     *      Port:   array[0..$1000] of byte; {i/o ports}
  198.     *      PortW:  array[0..$1000] of word;
  199.     *   type
  200.     *      string = array[1..255] of char;
  201.     *
  202.     *)
  203.  
  204.  
  205. \  /*
  206. \   * rename some tp4 identifiers that conflict with tc1.0 identifiers
  207. \   */
  208. \  #define intr    Pintr
  209. \  #define getdate Pgetdate
  210. \  #define gettime Pgettime
  211. \  #define setdate Psetdate
  212. \  #define settime Psettime
  213. \  #define keep    Pkeep
  214.  
  215.       
  216. (*
  217.  * Implementation of support procedures
  218.  *
  219.  *)
  220.  
  221. implementation
  222.  
  223. \  #include <stdlib.h>
  224. \  #include <string.h>
  225. \  #include <stdarg.h>
  226. \  #include <dos.h>
  227. \  #include <conio.h>
  228. \  #include <ctype.h>
  229. \  #include <alloc.h>
  230.  
  231.  
  232.    (*
  233.     * String/character concatenation function
  234.     *
  235.     * This function takes a sprintf-like control string, a variable number of
  236.     * parameters, and returns a pointer a static location where the processed
  237.     * string is to be stored.
  238.     *
  239.     *)
  240.  
  241. \  string scat(string control, ...)
  242. \  {
  243. \     static char buf[STRSIZ];
  244. \     char buf2[STRSIZ];
  245. \     va_list args;
  246. \
  247. \     va_start(args, control);     /* get variable arg pointer */
  248. \     vsprintf(buf2,control,args); /* format into buf with variable args */
  249. \     va_end(args);                /* finish the arglist */
  250. \
  251. \     strcpy(buf,buf2);
  252. \     return buf;                  /* return a pointer to the string */
  253. \  }
  254.  
  255.  
  256.    (*
  257.     * string build - like scat, sprintf, but will not over-write any
  258.     *                input parameters
  259.     *)
  260. \  void sbld(string dest,
  261. \            string control, ...)
  262. \  {
  263. \     char buf[STRSIZ];
  264. \     va_list args;
  265. \
  266. \     va_start(args, control);     /* get variable arg pointer */
  267. \     vsprintf(buf,control,args);  /* format into buf with variable args */
  268. \     va_end(args);                /* finish the arglist */
  269. \
  270. \     strcpy(dest,buf);            /* copy result */
  271. \  }
  272.  
  273.  
  274.  
  275.    (*
  276.     * spos(str1,str2) - returns index of first occurence of str1 within str2;
  277.     *    1=first char of str2
  278.     *    0=nomatch
  279.     *)
  280.    function spos(str1, str2: string): integer;
  281.       {returns the position of a substring within a longer string}
  282.    begin
  283. \     string res;
  284. \     res = strstr(str2,str1);
  285. \     if (res == NULL)
  286. \        return 0;
  287. \     else
  288. \        return res - str2 + 1;
  289.    end;
  290.  
  291.  
  292.    (*
  293.     * cpos(str1,str2) - returns index of first occurence of c within str2;
  294.     *    1=first char of str2
  295.     *    0=nomatch
  296.     *)
  297.    function cpos(c: char; str2: string): integer;
  298.       {returns the position of a character within a string}
  299.    begin
  300. \     string res;
  301. \     res = strchr(str2,c);
  302. \     if (res == NULL)
  303. \        return 0;
  304. \     else
  305. \        return res - str2 + 1;
  306.    end;
  307.  
  308.  
  309.    function Copy(s: string; from,len: integer): string;
  310.       {copy len bytes from the dynamic string dstr starting at position from}
  311.    begin
  312. \     static char buf[STRSIZ];
  313. \     buf[0]=0;
  314. \     if (from>strlen(s))       /* copy past end gives null string */
  315. \        return buf;
  316. \
  317. \     strcpy(buf,s+from-1);    /* skip over first part of string */
  318. \     buf[len] = 0;            /* truncate after len characters */
  319. \     return buf;
  320.    end;
  321.  
  322.    procedure Move(tomem, fmmem: pointer; bytes: word);
  323.    begin
  324. \     while (bytes--)
  325. \        *tomem++ = *fmmem++;
  326.    end;
  327.  
  328.    procedure FillChar(dest: pointer; size: integer; value: char);
  329.    begin
  330. \     while (size--)
  331. \        *dest++ = value;
  332.    end;
  333.  
  334.    function Length(s: string): integer;
  335.    begin
  336. \     return strlen(s);
  337.    end;
  338.  
  339.    function ctos(c: char): string;
  340.       {convert a character into a string}
  341.    begin
  342. \     static char s[2];
  343. \     s[0] = c;
  344. \     s[1] = 0;
  345. \     return s;
  346.    end;
  347.    
  348.    function UpCase(c: char): char;
  349.    begin
  350. \     if (islower(c))
  351. \        c = toupper(c);
  352.       UpCase := c;
  353.    end;
  354.  
  355.  
  356.    (*
  357.     * This function operate like fscanf except for an added control
  358.     * code used for full-line reads.
  359.     *
  360.     *)
  361. \  int tscanf(text *fd,
  362. \             string control, ...)
  363. \  {
  364. \     va_list args;
  365. \     string arg1;
  366. \     int     i;
  367. \
  368. \     va_start(args, control);     /* get variable arg pointer */
  369. \
  370. \     /* process special case for full-line reads (why doesn't scanf allow
  371. \        full-line string reads?  why don't gets and fgets work the same?) */
  372. \     if (*control == '#') {
  373. \        arg1 = va_arg(args,string);
  374. \        fgets(arg1,STRSIZ,fd->filevar);
  375. \        arg1[strlen(arg1)-1] = 0;
  376. \        return 1;
  377. \     }
  378. \
  379. \     /* pass the request on to fscanf */
  380. \     i = vfscanf(fd->filevar,control,args);    /* scan with variable args */
  381. \     va_end(args);                             /* finish the arglist */
  382. \
  383. \     return i;                        /* return a pointer to the string */
  384. \  }
  385.  
  386.  
  387. \  void tprintf(text *fd,
  388. \               string control, ...)
  389. \  {
  390. \     va_list args;
  391. \     va_start(args, control);               /* get variable arg pointer */
  392. \     vfprintf(fd->filevar,control,args);    /* scan with variable args */
  393. \     va_end(args);                          /* finish the arglist */
  394. \  }
  395.  
  396.       
  397.  
  398.    (* 
  399.     * Standard procedures and functions
  400.     *
  401.     *)
  402.    
  403.    function Sqr(n: real): real;
  404.    begin
  405.       Sqr := n * n;
  406.    end;
  407.  
  408.    function Trunc(r: real): longint;
  409.    begin
  410.       Trunc := longint(r);
  411.    end;
  412.  
  413.    function Round(r: real): real;
  414.    begin
  415.       Round := real(longint(r + 0.5));
  416.    end;
  417.  
  418.    function Pred(b: integer): integer;
  419.    begin
  420.       Pred := b - 1;
  421.    end;
  422.  
  423.    function Succ(b: integer): integer;
  424.    begin
  425.       Succ := b + 1;
  426.    end;
  427.  
  428.    function Ord(c: char): integer;
  429.    begin
  430.       Ord := integer(c);
  431.    end;
  432.  
  433.    function Hi(w: word): word;
  434.    begin
  435.       Hi := w shr 8;
  436.    end;
  437.  
  438.    function Lo(w: word): word;
  439.    begin
  440.       Lo := w and $FF;
  441.    end;
  442.  
  443.    procedure Inc(var b: byte);
  444.    begin
  445.       b := b + 1;
  446.    end;
  447.  
  448.    procedure Dec(var b: byte);
  449.    begin
  450.       b := b - 1;
  451.    end;
  452.  
  453.    function MemAvail: longint;
  454.    begin
  455. \     return coreleft();
  456.    end;
  457.  
  458.    function MaxAvail: longint;
  459.    begin
  460. \     return coreleft();
  461.    end;
  462.  
  463.    procedure Dispose(ptr: pointer);
  464.    begin
  465. \     free(ptr);
  466.    end;
  467.  
  468.    procedure FreeMem(ptr: pointer; size: integer) as PfreeMem;
  469.    begin
  470. \     free(ptr);     
  471.    end;
  472.  
  473.    function ParamStr(n: integer): string;
  474.    begin
  475. \     return tptc_argv[n];
  476.    end;
  477.    
  478.    procedure Delete(s: string; posit,number: integer);
  479.    begin
  480. \     strcpy(s+posit-1,s+posit+number-2);
  481.    end;
  482.  
  483.    procedure Val(s: string; var res: real; var code: integer);
  484.    begin
  485.       res := atof(s);
  486.       code := 0;
  487.    end;
  488.  
  489.    function Int(r: real): real            as dint;
  490.    begin
  491.       Int := real(longint(r));
  492.    end;
  493.  
  494.  
  495.    (*
  496.     * These will probably change when binary file translation is implemented
  497.     *
  498.     *)
  499.  
  500.    function Eof(var fd: text): boolean;
  501.    begin
  502. \     return feof(fd->filevar);
  503.    end;
  504.  
  505.    procedure Flush(var fd: text);
  506.    begin
  507. \     fflush(fd->filevar);
  508.    end;
  509.  
  510.    procedure Close(var fd: text);
  511.    begin
  512. \     fclose(fd->filevar);
  513.    end;
  514.  
  515.    procedure Assign(var fd: text; name: string);
  516.    begin
  517.       fd.fname := name;
  518.    end;
  519.  
  520.    procedure Reset(var fd: text);
  521.    begin
  522. \     fd->filevar = fopen(fd->fname,"r");
  523. \     ioresult = (fd->filevar) == NULL;
  524.    end;
  525.  
  526.    procedure ReWrite(var fd: text);
  527.    begin
  528. \     fd->filevar = fopen(fd->fname,"w");
  529. \     ioresult = (fd->filevar) == NULL;
  530.    end;
  531.    
  532.    procedure Append(var fd: text);
  533.    begin
  534. \     fd->filevar = fopen(fd->fname,"a");
  535. \     ioresult = (fd->filevar) == NULL;
  536.    end;
  537.  
  538.    procedure SetTextBuf(var fd: text; buffer: pointer; size: word);
  539.    begin
  540. \     setvbuf(fd->filevar,buffer,_IOFBF,size);
  541.    end;
  542.  
  543.    procedure Seek(var fd: text; rec: word);
  544.    begin
  545.       {stubbed}
  546.    end;
  547.  
  548.    function SeekEof(var fd: text): boolean;
  549.    begin
  550.       {stubbed}
  551.    end;
  552.  
  553.  
  554.  
  555.    (* 
  556.     * Additional procedures called by translated code 
  557.     *
  558.     *)
  559.    
  560. \  setrec setof(byte element,...)
  561. \  {
  562. \     /* stubbed */
  563. \  }
  564.  
  565.    function inset(item: byte; theset: setrec): boolean;
  566.    begin
  567.       {stubbed}
  568.    end;
  569.  
  570.  
  571. (*
  572.  * Top-level initialization
  573.  *
  574.  *)
  575. begin
  576. \  tptc_argv = argv;
  577. \  paramcount = argc;
  578. \  cseg = _CS;
  579. \  sseg = _SS;
  580. \  dseg = _DS;
  581. \ /* input.filevar = stdin; */
  582. \ /* output.filevar = stdout; */
  583. end.
  584.  
  585.